package cn.stylefeng.guns.modular.time.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;

import org.springframework.stereotype.Service;
import cn.stylefeng.guns.modular.time.entity.Tasks;
import cn.stylefeng.guns.modular.time.mapper.TasksMapper;
import cn.stylefeng.guns.modular.time.model.request.TasksRequest;
import cn.stylefeng.guns.modular.time.service.TasksService;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;

/**
 * 任务表业务实现层
 *
 * @author 叶新华
 * @date 2025/04/17 23:18
 */
@Service
public class TasksServiceImpl extends ServiceImpl<TasksMapper, Tasks> implements TasksService {

    @Override
    public PageResult<Tasks> findPage(TasksRequest tasksRequest) {
        // 构建查询条件
        LambdaQueryWrapper<Tasks> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tasks::getDelFlag, "N")
                .eq(Tasks::getUserId, LoginContext.me().getLoginUser().getUserId())
                .eq(Tasks::getStatus, tasksRequest.getCompleted() != null ? (tasksRequest.getCompleted() ? "已完成" : "未完成") : "未完成");
        
        // 添加搜索条件
        if (tasksRequest.getSearchText() != null && !tasksRequest.getSearchText().trim().isEmpty()) {
            String searchText = tasksRequest.getSearchText().trim();
            queryWrapper.and(wrapper -> wrapper
                .like(Tasks::getTitle, searchText)
                .or()
                .like(Tasks::getDescription, searchText)
            );
        }
        // 优先级筛选
        if (tasksRequest.getPriority() != null && !tasksRequest.getPriority().trim().isEmpty()) {
            queryWrapper.eq(Tasks::getPriority, tasksRequest.getPriority().trim());
        }
        
        // 首先按置顶状态降序排序
        queryWrapper.orderByDesc(Tasks::getPinned);
        queryWrapper.orderByDesc(Tasks::getCreateTime);

        // 添加其他排序条件
        if ("dueDate".equals(tasksRequest.getSortField())) {
            queryWrapper.orderBy(true, "asc".equals(tasksRequest.getSortOrder()), 
                    Tasks::getDueDate);
        } else if ("priority".equals(tasksRequest.getSortField())) {
            queryWrapper.orderBy(true, "asc".equals(tasksRequest.getSortOrder()), 
                    Tasks::getPriority);
        } else {
            // 默认按创建时间倒序排序
            queryWrapper.orderByDesc(Tasks::getCreateTime);
        }
        
        // 分页查询
        Page<Tasks> page = this.page(new Page<>(tasksRequest.getPageNo(), tasksRequest.getPageSize()), queryWrapper);
        // 返回结果
        return PageResultFactory.createPageResult(page);
    }

    @Override
    public void add(TasksRequest tasksRequest) {
        Tasks tasks = new Tasks();
        tasks.setTitle(tasksRequest.getTitle());
        tasks.setDescription(tasksRequest.getDescription());
        tasks.setDueDate(tasksRequest.getDueDate());
        tasks.setPriority(tasksRequest.getPriority());
        tasks.setStatus("未完成");
        tasks.setUserId(LoginContext.me().getLoginUser().getUserId());
        tasks.setPinned("N");
        tasks.setCreateTime(new Date());
        this.save(tasks);
    }

    @Override
    public void edit(TasksRequest tasksRequest) {
        Tasks tasks = this.getById(tasksRequest.getTaskId());
        if (tasks != null) {
            tasks.setTitle(tasksRequest.getTitle());
            tasks.setDescription(tasksRequest.getDescription());
            tasks.setDueDate(tasksRequest.getDueDate());
            tasks.setPriority(tasksRequest.getPriority());
            tasks.setUpdateTime(new Date());
            this.updateById(tasks);
        }
    }

    @Override
    public void delete(TasksRequest tasksRequest) {
        this.removeById(tasksRequest.getTaskId());
    }

    @Override
    public void updateStatus(TasksRequest tasksRequest) {
        Tasks tasks = this.getById(tasksRequest.getTaskId());
        if (tasks != null) {
            tasks.setStatus(tasksRequest.getCompleted() ? "已完成" : "未完成");
            this.updateById(tasks);
        }
    }
}
